package random.fox.p2p.presenter;

import android.text.TextUtils;

import random.fox.p2p.interactors.OnFinishRegisterListener;
import random.fox.p2p.interactors.RegisterInteractor;
import random.fox.p2p.util.Util;
import random.fox.p2p.view.IRegisterView;

/**
 * Created by wq on 15/3/19.
 */
public class RegisterPresenterImpl implements RegisterPresenter, OnFinishRegisterListener {
    private String mCode;
    private IRegisterView registerView;
    private RegisterInteractor interactor;

    public RegisterPresenterImpl(IRegisterView registerView, RegisterInteractor interactor) {
        this.registerView = registerView;
        this.interactor = interactor;
    }


    @Override
    public void checkPhone(String phoneNumber) {
        if (Util.isPhoneNumber(phoneNumber)) {
            registerView.showProgress();
            interactor.savePhoneNumber(phoneNumber, this);
        } else {
            registerView.errorPhone();
        }
    }

    @Override
    public void checkCode(String code) {
        if (TextUtils.isEmpty(code)) {
            registerView.showMessage("请输入验证码");
            return;
        }

        if (TextUtils.isEmpty(mCode)) {
            registerView.errorCode();
            return;
        }
        if (code.equals(mCode)) {
            registerView.goToNext();
        } else {
            registerView.errorCode();
        }

    }

    @Override
    public void setPassword(String password) {
        if (TextUtils.isEmpty(password)){
            registerView.showMessage("请设置密码");
            return;
        }

        if (!Util.checkPassword(password)){
            registerView.showMessage("请按规则设置密码");
            return;
        }

        registerView.showProgress();
        interactor.savePassword(password,this);
    }

    @Override
    public void onGetCode(String code) {
        mCode = code;
        registerView.hideProgress();
        registerView.showCode();
    }

    @Override
    public void onGetUid(String uid) {
        registerView.hideProgress();
        registerView.goToNext();
    }

    @Override
    public void onError(Throwable throwable) {
        registerView.hideProgress();
        registerView.showMessage("出错了");
    }
}
